package com.example.zsf.base;

import android.os.Bundle;
import android.os.IBinder;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zsf.view.ProgressiveDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;

public class ZSFBaseActivity extends AppCompatActivity {

    protected ProgressiveDialog progressiveDialog;
    private List<Call> calls = new ArrayList<>();

    //<editor-fold desc=”life cycle”>


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressiveDialog = new ProgressiveDialog(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideKeyboard();
    }


    @Override
    protected void onDestroy() {
        for (Call call : calls) {
            call.cancel();
        }
        super.onDestroy();
    }

    //</editor-fold>

    //<editor-fold desc=”protected method”>

    /**
     * 把call添加到calls里面,方便在destroy的时候注销
     *
     * @param call .
     */
    public void disposed(Call call) {
        calls.add(call);
    }


    //</editor-fold>


    public void hideKeyboard() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager manager = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
                if (ZSFBaseActivity.this.getCurrentFocus() == null) {
                    return;
                }
                IBinder windowToken = ZSFBaseActivity.this.getCurrentFocus().getWindowToken();
                if (manager != null) {
                    manager.hideSoftInputFromWindow(
                            windowToken,
                            InputMethodManager.HIDE_NOT_ALWAYS
                    );
                }
            }
        }, 100);
    }
}
